﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using G28BS.Models;
using WebMatrix.WebData;
using G28BS.ViewModels;

namespace G28BS.Controllers
{
    [HandleError]
    public class HomeController : BaseAppController
    {
        private IAppRepository rep = null;

        public HomeController()
        {
            rep = new AppRepository();
        }

        public HomeController(IAppRepository r)
        {
            rep = r;
        }

        public ActionResult Index()
        {
            // We are using ViewModel for data to display on pages.
            var ViewModel = new HomeIndexViewModel { appRepo = rep};
            // Hardcoded at the moment.
            ViewModel.highScorePlayer = "David";
            ViewModel.mostGamesWon = "Eggert";
            // --------------------------------
            ViewModel.mostGamesPlayed = _MostGamesPlayed();
            ViewModel.gameRoomsInNeedOfPlayers = _GameRoomsInNeedForPlayers(_GameRoomsIdInNeedForPlayers());
            ViewModel.activeGameRooms = _ActiveGameRooms();
            return View(ViewModel);
        }

        public ActionResult About()
        {
            ViewBag.Message = "Um okkur síðan";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Hafðu samband síðan (láttu mig samt bara í friði)";

            return View();

        }

        public ActionResult NotFound()
        {
            ViewBag.Message = "Ertu að villast væni minn?";

            return View();

        }

        
        public ActionResult GameDetails(int id)
        {
            // var playerRank = (from x in rep.gameResults where x.gameTypeGameID == id orderby x.score select x).Take(5);
            var ViewModel = new GameDetailsViewModel
            {
                game = (from x in rep.games where x.gameID == id select x).First(),
                waitingGames = (from x in rep.gameRooms where x.state == GameRoomStates.Waiting && x.gameID == id select x)
            };
            return View(ViewModel);
        }

        // Find the user with most games played.
        // Returns the user name or message that no one has played.
        private string _MostGamesPlayed()
        { 
            var mostGamesPlayed = (from x in rep.userProfiles 
                                   orderby x.gameCount descending 
                                   select x.UserName).FirstOrDefault(); 
            if(string.IsNullOrEmpty(mostGamesPlayed))
            {
                return "Enginn hefur spilað!";
            }
            return mostGamesPlayed;
        }

        // Returns list of GameRooms that needs players.
        private List<GameRoom> _GameRoomsInNeedForPlayers(IEnumerable<int> gameRoomIds)
        {
            List<GameRoom> gameRoomsInNeed = (from gr in rep.gameRooms
                    where gameRoomIds.Contains(gr.gameRoomID)
                    where gr.state == GameRoomStates.Waiting
                    select gr).ToList();

            return gameRoomsInNeed;
        }

       
        // Returns gameRoomsID of GameRooms that needs players.
        private IEnumerable<int> _GameRoomsIdInNeedForPlayers()
        {
            var gameRoomsInNeed = (from x in rep.slots 
                                   where x.occupied == false 
                                   select x.gameRoomID).ToList().Distinct();
            return gameRoomsInNeed;
        }

        // Returns list of Active GameRooms
        private IEnumerable<GameRoom> _ActiveGameRooms()
        {
            var activeGameRooms = (from x in rep.gameRooms 
                                   where x.state == GameRoomStates.Active 
                                   select x);
            
            return activeGameRooms;
        }

    }
}
